Linear Algebra Review

Python is a great programming language and with the help of a few popular libraries such numpy and matplotlib it becomes a powerful environment for Linear Algebra computing.

Numpy Documentation - This brief overview has touched on many of the important things that you need to know about numpy, but is far from complete. Check out the numpy reference to find out much more about numpy.

1) Matrices and Vectors

Rectangulare array of numbers

Numpy Array


In [221]:
import numpy as np
1) Matrices and Vectors

A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers.


In [222]:
# Create a rank 1 array
a = np.array(
    [
        [1,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ]
)  
print (a)
print (type(a))            
print (a.shape)


[[1 2 3 4]
 [1 4 5 6]
 [1 2 3 4]]
<class 'numpy.ndarray'>
(3, 4)
1.1) Change an element of the array

In [223]:
a = np.array(
    [
        [1,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ])
print (a)
a[0,0] = 100
print ()
print (a)


[[1 2 3 4]
 [1 4 5 6]
 [1 2 3 4]]

[[100   2   3   4]
 [  1   4   5   6]
 [  1   2   3   4]]
1.2) Numpy functions

Datatypes


In [224]:
a = np.array(
    [
        [1,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ]
)
print (a)
print (type(a[0,0]))
print ()
a = np.array(
    [
        [1.2,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ]
)  
print (a)
print (type(a[0,0]))


[[1 2 3 4]
 [1 4 5 6]
 [1 2 3 4]]
<class 'numpy.int64'>

[[ 1.2  2.   3.   4. ]
 [ 1.   4.   5.   6. ]
 [ 1.   2.   3.   4. ]]
<class 'numpy.float64'>

Compute sum of all elements in matrix


In [225]:
x = np.array([
        [1,2,1],[3,4,1]])

print (x)
print (np.sum(x))  
print (np.sum(x, axis=0))  
print (np.sum(x, axis=1))


[[1 2 1]
 [3 4 1]]
12
[4 6 2]
[4 8]

Create an array of all zeros


In [226]:
a = np.zeros((4,4))  
a


Out[226]:
array([[ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.]])

Create an array of all ones


In [227]:
a = np.ones((2,5))   
a


Out[227]:
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])

Create a constant array


In [228]:
a = np.full((4,4), 7)  
a


/Users/paulrad/anaconda/lib/python3.5/site-packages/numpy/core/numeric.py:301: FutureWarning: in the future, full((4, 4), 7) will return an array of dtype('int64')
  format(shape, fill_value, array(fill_value).dtype), FutureWarning)
Out[228]:
array([[ 7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.],
       [ 7.,  7.,  7.,  7.]])

Create an identity matrix


In [229]:
a = np.eye(4)       
a


Out[229]:
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])

Create an array filled with random values


In [230]:
a = np.random.random((5,5)) 
a


Out[230]:
array([[ 0.99675889,  0.48221569,  0.74618279,  0.78806924,  0.91611288],
       [ 0.55395189,  0.66142647,  0.79202116,  0.45990973,  0.05020694],
       [ 0.80307842,  0.76407037,  0.40752493,  0.39043778,  0.2128692 ],
       [ 0.15180235,  0.48343888,  0.30267939,  0.2862789 ,  0.14557521],
       [ 0.33909821,  0.52614965,  0.74773651,  0.21944924,  0.39060696]])

slicing to pull out the subarray


In [231]:
a = np.array(
    [
        [1,2,3,4],
        [5,6,7,8],
        [9,10,11,12]
    ])
print (a)
print ()
b = a[:1, :3]
print (b)


[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

[[1 2 3]]
2) Matrix Addition


In [232]:
x = np.ones((4,7))
print (x)
y = np.full((4,7), 4.3)
print (y)
z = x+y
print ()
print (z)


[[ 1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.]
 [ 1.  1.  1.  1.  1.  1.  1.]]
[[ 4.3  4.3  4.3  4.3  4.3  4.3  4.3]
 [ 4.3  4.3  4.3  4.3  4.3  4.3  4.3]
 [ 4.3  4.3  4.3  4.3  4.3  4.3  4.3]
 [ 4.3  4.3  4.3  4.3  4.3  4.3  4.3]]

[[ 5.3  5.3  5.3  5.3  5.3  5.3  5.3]
 [ 5.3  5.3  5.3  5.3  5.3  5.3  5.3]
 [ 5.3  5.3  5.3  5.3  5.3  5.3  5.3]
 [ 5.3  5.3  5.3  5.3  5.3  5.3  5.3]]

In [233]:
x = np.array(
    [
        [1.2,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ])
print (x)
y = np.array(
    [
        [1,2,3,4],
        [1,4,5,6],
        [1,2,3,4]
    ])
print (y)
print ()
z = np.add(x, y)
print (z)


[[ 1.2  2.   3.   4. ]
 [ 1.   4.   5.   6. ]
 [ 1.   2.   3.   4. ]]
[[1 2 3 4]
 [1 4 5 6]
 [1 2 3 4]]

[[  2.2   4.    6.    8. ]
 [  2.    8.   10.   12. ]
 [  2.    4.    6.    8. ]]
2) Matrix Subtraction


In [234]:
z = x - y
print (z)
print ()
z = np.subtract(x, y)
print (z)


[[ 0.2  0.   0.   0. ]
 [ 0.   0.   0.   0. ]
 [ 0.   0.   0.   0. ]]

[[ 0.2  0.   0.   0. ]
 [ 0.   0.   0.   0. ]
 [ 0.   0.   0.   0. ]]
3) Matrix Vector Multiplication


In [235]:
x = np.array(
    [
        [1,0,-1],
        [1,2,-1],
        [1,0,-1],
        [1,0,-1]
    ])
print (x)
y = np.array([1,1,2])
print (y)
z = np.dot(x, y)
print (z)


[[ 1  0 -1]
 [ 1  2 -1]
 [ 1  0 -1]
 [ 1  0 -1]]
[1 1 2]
[-1  1 -1 -1]
3.1) Matrix Multiplication


In [236]:
x = np.array(
    [
        [1,2,3],
        [4,5,6]
    ])
print (x)
y = np.array(
    [
        [7,8],
        [9,10],
        [11,12]
    ])
print (y)
print ()
z = np.dot(x, y)
print (z)
print ()
z = np.dot(y, x)
print (z)


[[1 2 3]
 [4 5 6]]
[[ 7  8]
 [ 9 10]
 [11 12]]

[[ 58  64]
 [139 154]]

[[ 39  54  69]
 [ 49  68  87]
 [ 59  82 105]]
4) Matrix Transpose


In [237]:
x = np.array([[0,1,2,0], [0,3,4,5]])
print (x) 
print ()
print (x.T)


[[0 1 2 0]
 [0 3 4 5]]

[[0 0]
 [1 3]
 [2 4]
 [0 5]]

Note that taking the transpose of a rank 1 array does nothing


In [238]:
x = np.array([0,1,2,3])
print (x)
print (x.shape)
y = x.T
print (y.shape)


[0 1 2 3]
(4,)
(4,)

Create an empty matrix with the same shape as x


In [240]:
x = np.array([[1,2,3], [4,5,6], [7,8,9], [10, 11, 12]])
print (x)
print ()
v = np.array([1, 5, 1])
y = np.empty_like(x)   

for i in range(4):
    y[i, :] = x[i, :] + v

print (y)


[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

[[ 2  7  4]
 [ 5 10  7]
 [ 8 13 10]
 [11 16 13]]

In [ ]: